<?php
class Post extends Eloquent {
	
	protected $guarded = array('id');
	
	public static $rules = [
		'title' => 'required',
		'content' => 'required',
	];
	
	/**
	 * Get results by page
	 *
	 * @param int $page
	 * @param int $limit
	 * @return StdClass
	 */
	public function getPostByPage($page = 1, $limit = 10)
	{
		$results = new StdClass;
		$results->page = $page;
		$results->limit = $limit;
		$results->totalItems = 0;
		$results->items = array();
			
		$offset = $limit * ($page - 1);
		$posts = DB::select("
				SELECT * FROM posts AS p WHERE id IN (5, 4)
				UNION ALL
				SELECT * FROM posts AS p WHERE id IN (3, 6)
				ORDER BY id
				LIMIT $offset, $limit
				");
	
				$count = DB::select('
			SELECT COUNT(*) AS cnt
			FROM
			(
				SELECT * FROM posts AS p WHERE id IN (3, 4)
				UNION ALL
				SELECT * FROM posts AS p WHERE id IN (5, 6)
			) unionTbl
		');
	
		$results->totalItems = $count[0]->cnt;
		$results->items = $posts;
		//debug($results);
	
		return $results;
	}

}
